const express = require('express'); 
const pool = require('../pool');
const router = express.Router();

router.get('/list',(req,res,next)=>{
    let obj = req.obj;
    console.log(obj)
    pool.query('select img,count,src,customer_cat.cname,customer_cat.cid from customer,customer_cat where c_id=cid',(err,r)=>{
        if(err){
            return next(err)
        };
        res.send({code:200,msg:'',data:reArr(r)});
    })
})
router.get('/dail',(req,res,next)=>{
    let obj = req.obj;
    console.log(obj);
    pool.query('select count from customer_dail',(err,r)=>{
        if(err){
            return next(err)
        };
        res.send({code:200,msg:'',data:r});
    });
});

function reArr(r){
    let obj = { };
    let arr = [];
    let b = [];
    let num = 0;
    r.forEach((v,i)=>{
        if(num!=v.cid){
            i++;
            arr = [];
            obj = {};
            let o = {
                count:v.count,
                img:v.img,
                src:v.src
            };
            arr.push(o);
            obj.title = v.cname;
            obj.msg = arr;
            b.push(obj)
        }else{
            let o = {
                title:v.pname,
                count:v.count,
                img:v.img,
                src:v.src
            };
            arr.push(o);
        }
        num=v.cid;
       
    });
   
    return b;
}
module.exports = router;